/**
 * [LupMisNotAllowedSync]
 * 本代码为系统自动生成代码，请根据自己业务进行修改;
 * 生成时间 2022-02-02 11:18:21;
 * 版权所有 2020-2022 lizhongwen，并保留所有权利;
 * 说明: 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；不允许对程序代码以任何形式任何目的的再发布;
 * 作者: 中文Lee;
 * 作者主页: http://www.lizhongwen.com;
 * 邮箱: 360811363@qq.com;
 * QQ: 360811363;
 */

var setting = {
    check: {
        enable: true
    },
    data: {
        simpleData: {
            enable: true
        }
    }
};
var treeObj;
var pageActionObj;

function getTreeListNodes() {
    var treeIdsArray = new Array();
    var nodes = treeObj.getCheckedNodes();
    for (var i = 0; i < nodes.length; i++) {
        var type = nodes[i].type;
        treeIdsArray.push(nodes[i].id);
    }
    var treeIds = treeIdsArray.join(',');
    $("#tree_ids").val(treeIds);
}

function getPageRoleListNodes() {
    var pageIdArray = new Array();
    var actionIdArray = new Array();
    var nodes = pageActionObj.getCheckedNodes();
    if (nodes.length == 0) {
        $("#page_ids").val("");
        return false;
    }
    for (var i = 0; i < nodes.length; i++) {
        var type = nodes[i].type;
        if (type == 'page') {
            pageIdArray.push(nodes[i].id); //页面id
        } else if (type == 'action') {
            actionIdArray.push(nodes[i].id); //页面id
        }
    }
    var pageIds = pageIdArray.join(',');
    var actionIds = actionIdArray.join(',');
    if (actionIds != "") {
        var arr = actionIds.split(",");
        var json = {};
        for (var i = 0; i < arr.length; i++) {
            var arr1 = arr[i].split("@");
            var pageId = arr1[0];
            var actionId = arr1[1];
            json[pageId] = [];
        }
        for (var val in json) {
            var actionIdArr = new Array();
            for (var j = 0; j < arr.length; j++) {
                var arr1 = arr[j].split("@");
                var pageId = arr1[0];
                var actionId = arr1[1];
                if (val == pageId && actionIdArr.indexOf(actionId) == -1) {
                    actionIdArr.push(actionId);
                }
            }
            json[val] = actionIdArr;
        }
        $("#page_ids").val(JSON.stringify(json));
    }
}

function setData() {
    getTreeListNodes();
    getPageRoleListNodes();
}

// var node = pageActionObj.getNodeByParam("id", "6547315ff7884b7ca171a67bfd289018");
// node.checked = true;
// zTree.updateNode(node); 
layui.use(['layer', 'table', 'form'], function() {
    ajaxStart();
    //统一给ajax请求加header参数
    ajaxSend();
    //截获ajax参数
    ajaxSetup();
    table = layui.table;
    form = layui.form;
    layui.form.on('switch(status)', function(data) {
        if (this.checked) {
            layui.jquery("input[name='status']").val(1);
        } else {
            layui.jquery("input[name='status']").val(-1);
        }
        // layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
    });
    //查询数据
    var query = getQueryString("query");
    var id = getQueryString("id");

    layui.jquery("input[name='id']").val(id);

    var url = base_gateway + query;
    request(url, { role_id: id, id: id }, function(ret) {
        var code = ret.code;
        console.log(ret);
        if (code == 200) {
            document.getElementById('form-list').innerHTML = baidu.template('tpl-list', ret);
            layui.form.render(null, null);
            //查询菜单树
            request(base_gateway + "sys/tree/lists", { role_id: id }, function(ret) {
                if (ret.code == 200) {
                    var zNodes = [];
                    var rows = ret.data.rows || ret.data;
                    for (var i = 0; i < rows.length; i++) {
                        var open = false;
                        if (ret.data[i].pid == "0") {
                            open = true;
                        }
                        var row = { id: rows[i].id, checked: rows[i].checked, name: rows[i].tree_name, pId: rows[i].pid, open: open };
                        zNodes.push(row);
                    }
                    setting.check.chkboxType = { "Y": "ps", "N": "ps" };
                    treeObj = $.fn.zTree.init($("#treeList"), setting, zNodes);
                    //开始判断是否需要选中该菜单树
                    // var node = pageActionObj.getNodeByParam("id", "6547315ff7884b7ca171a67bfd289018");
                    // node.checked = true;
                    // pageActionObj.updateNode(node);
                } else {
                    layer.msg(ret.msg, {
                        shade: [0.1, '#393D49'],
                        time: 1500,
                        icon: 5
                    });
                }
            });


            request(base_gateway + "sys/page/lists", { role_id: getQueryString("id"),'page':1,'limit':9999 }, function(ret) {
                if (ret.code == 200) {
                    var zNodes = [];
                    var rows = ret.data.rows || ret.data;
                    for (var i = 0; i < rows.length; i++) {
                        var row = {
                            id: rows[i].id,
                            type: "page",
                            name: rows[i].page_name + "【" + rows[i].code + "】",
                            pId: "0",
                            checked: rows[i].checked,
                            open: false
                        };
                        zNodes.push(row);
                        for (var j = 0; j < rows[i].action.length; j++) {
                            var rowAction = { id: rows[i].action[j].id, type: "action", name: rows[i].action[j].action_name + "【" + rows[i].action[j].code + "】", pId: rows[i].id, checked: rows[i].action[j].checked, open: false };
                            zNodes.push(rowAction);
                        }
                    }
                    // console.log(zNodes);
                    setting.check.chkboxType = { "Y": "ps", "N": "ps" };
                    pageActionObj = $.fn.zTree.init($("#pageActionList"), setting, zNodes);

                    //开始判断是否需要选中该菜单树
                    // var node = pageActionObj.getNodeByParam("id", "6547315ff7884b7ca171a67bfd289018");
                    // node.checked = true;
                    // pageActionObj.updateNode(node);


                } else {
                    layer.msg(ret.msg, {
                        shade: [0.1, '#393D49'],
                        time: 1500,
                        icon: 5
                    });
                }
            });

            // layui.jquery("input[name='tableName']").val(ret.data.tableName);
            // layui.jquery("input[name='oldTableName']").val(ret.data.tableName);
            // layui.jquery("input[name='tableRemarks']").val(ret.data.tableRemarks);
            // layui.jquery("input[name='sort']").val(ret.data.sort);
        }
    });

    var gateway = getQueryString("gateway");
    frameSubmit(base_gateway + gateway, true, function(ret) {

    });
});